#!/bin/bash

if [[ ! -e /tmp/checkoms4azurepre ]];then
  echo "File does not exist"
  touch /tmp/checkoms4azurepre
  echo 1 > /tmp/checkoms4azurepre
fi
podnum=`kubectl get pod -n oms4-pre  --kubeconfig=/root/.kube/config-y3pre|wc -l`
if [[ $podnum -ne 0 && `cat /tmp/checkoms4azurepre` -eq 0 ]];then
  echo "stop lova pre env and start cs pre env"
  for i in `kubectl get deployments -n tlsmt-pre --kubeconfig=/root/.kube/config-y3pre|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $i --replicas=0 -n tlsmt-pre --kubeconfig=/root/.kube/config-y3pre
  done
  for j in `kubectl get deployments -n cs-pre --kubeconfig=/root/.kube/config-y3pre|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $j --replicas=1 -n cs-pre --kubeconfig=/root/.kube/config-y3pre
  done
  echo 1 > /tmp/checkoms4azurepre
elif [[ $podnum -eq 0 && `cat /tmp/checkoms4azurepre` -eq 1 ]];then
  echo "start lova pre env and stop cs pre env"
  for i in `kubectl get deployments -n tlsmt-pre --kubeconfig=/root/.kube/config-y3pre|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $i --replicas=1 -n tlsmt-pre --kubeconfig=/root/.kube/config-y3pre
  done
  for j in `kubectl get deployments -n cs-pre --kubeconfig=/root/.kube/config-y3pre|awk '{print $1}'|awk 'NR>1'`
  do
     kubectl scale deployment $j --replicas=0 -n cs-pre --kubeconfig=/root/.kube/config-y3pre
  done
  echo 0 > /tmp/checkoms4azurepre
fi
